#!/usr/bin/env python
#
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import util
from models import Video, Videoactive
import logging
from django.utils import simplejson as json


class MainHandler(webapp.RequestHandler):
  def get(self):
    isactive = db.GqlQuery("SELECT * FROM Videoactive LIMIT 1")
    result = isactive.get()
    if result.active == "1":
      videos = db.GqlQuery("SELECT * FROM Video ORDER BY added DESC LIMIT 1")
      videoinfo=[{"id" :videos[0].videoid, "message": videos[0].videomessage}]
      b = json.dumps(videoinfo)
      self.response.out.write(b)
      result.active = "0"
      result.put()
    else:
      self.response.out.write("no")

application = webapp.WSGIApplication([('/retrieve', MainHandler)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

